package com.cyf.cheyichong.controller;

import com.cyf.cheyichong.commonutils.R;
import com.cyf.cheyichong.entity.User;
import com.cyf.cheyichong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/login")
    public R login(User user) {
        if (user.getUserPhone() == null)
            return R.error().message("手机号不可为空！");
        return userService.login(user);
    }

    @PostMapping(value = "/regist")
    public R regist(User user) {
        if (user.getUserPhone() == null)
            return R.error().message("手机号不可为空！");

        String tempId = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0, 11);
        user.setId(tempId);

        return userService.regist(user);
    }
}